<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/20
 * Time: 11:35
 */

namespace app\admin\controller;


use think\Request;

class Article extends Base
{
    private $obj;
    public function _initialize()
    {
        $this -> obj = model('Article');
    }

    public function index()
    {
        //获取文章的相关信息
        $artices = $this -> obj -> getArticlesInfo();
        return $this -> fetch('',[
            'articles'=> $artices
        ]);
    }
    public function add()
    {
        $tags = model('Tag') -> getTagsInfo();
        return $this -> fetch('',[
            'tags' => $tags
        ]);
    }

    public function save()
    {
        $data = input('post.');
        //验证信息是否正确
        $validate = validate('Article');
        if (!$validate -> check($data)){
            $this -> error($validate ->getError());
        }
        $res = $this->obj->add($data);
        if ($res){
            //如果添加成功,把tag的标签添加到article_tag表中,一堆多的关系
            //获取添加的id
            $resutlt = model('ArticleTag') -> add($res,$data['tag']);
            if ($resutlt){
                $this -> success('添加成功');
            }else{
                $this -> error('标签添加失败');
            }
        }else{
            $this -> error('添加失败');
        }
        //var_dump($data);
    }

    //修改
    public function edit($id)
    {
        if (intval($id) <1){
            $this -> error('参数错误');
        }
        $article = $this->obj->get($id);

        $tags = model('Tag') -> getTagsInfo();

        return $this -> fetch('',[
            'article' => $article,
            'tags' => $tags
        ]);
    }

    public function detail($id=0)
    {
        //获取相关信息

        return $this -> fetch('',[
//            'article' => article
        ]);
    }


}